package com.youjia.center.boot.data.mp.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.youjia.center.boot.data.mp.intercept.QueryInterceptor;
import com.youjia.center.boot.data.mp.intercept.PaginationInterceptor;
import com.youjia.center.boot.data.mp.intercept.SqlLogInterceptor;
import org.apache.commons.lang3.ObjectUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;

/**
 * 配置mp
 * @author fengjiansong
 * @date 2022.09.27
 */
@Configuration
@MapperScan("com.youjiajk.center.**.mapper.**")
public class MyBatisPlusConfig {

    @Value("${spring.datasource.druid.mp.page.max-result:500}")
    private Long maxResult;

    @Value("${spring.datasource.druid.mp.page.overflow:false}")
    private Boolean overflow;

    /**
     * 拦截器合集
     *
     * @param queryInterceptors 查询拦截器
     * @return mp拦截器
     */
    @Bean
    @ConditionalOnMissingBean(MybatisPlusInterceptor.class)
    public MybatisPlusInterceptor mybatisPlusInterceptor(ObjectProvider<QueryInterceptor[]> queryInterceptors) {

        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 配置分页拦截器
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();

        // 配置自定义查询拦截器
        QueryInterceptor[] queryInterceptorArray = queryInterceptors.getIfAvailable();

        if (ObjectUtils.isNotEmpty(queryInterceptorArray)) {
            AnnotationAwareOrderComparator.sort(queryInterceptorArray);
            paginationInterceptor.setQueryInterceptors(queryInterceptorArray);
        }

        // 设置最大分页返回数量
        paginationInterceptor.setMaxLimit(maxResult);

        // 设置请求的页面大于最大页后操作,true调回到首页,false 继续请求  默认false
        paginationInterceptor.setOverflow(overflow);

        interceptor.addInnerInterceptor(paginationInterceptor);
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;

    }

    /**
     * sql打印
     * @return SqlLogInterceptor
     */
    @Bean
    public SqlLogInterceptor sqlLogInterceptor() {
        return new SqlLogInterceptor();
    }

    /**
     * 下划线转驼峰
     */
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return configuration -> configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
    }

}
